//
//  YTETAILogController.m
//  yitaicaijing
//
//  Created by DIANGHONG on 2018/9/11.
//  Copyright © 2018年 DIANGHONG. All rights reserved.
//

#import "YTETAILogController.h"
#import "YTETAILogHeaderView.h"
#import "YTETAILogCell.h"
@interface YTETAILogController ()<UITableViewDataSource>

@property(nonatomic ,strong)YTETAILogHeaderView *headerView;
@property(nonatomic ,strong)UITableView *tableView;

@property(nonatomic ,strong)YTETAILogModel *model;

@property(nonatomic ,assign)int page;
@end

@implementation YTETAILogController

// lazy headerView
- (YTETAILogHeaderView *)headerView
{
    if (!_headerView) {
        _headerView = [YTETAILogHeaderView headerViewWithFrame:CGRectMake(0, 0, kScreenWidth, 44)];
    }
    return _headerView;
}

// lazy tableView
- (UITableView *)tableView
{
    if (!_tableView) {
        CGFloat tableViewY = CGRectGetMaxY(self.headerView.frame);
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, tableViewY, kScreenWidth, kScreenHeight - tableViewY -(KIsiPhoneX ? 88 : 64))];
        _tableView.rowHeight = 44;
        _tableView.dataSource = self;
        
        
        YTRefreshHeader *header = [YTRefreshHeader headerWithRefreshingBlock:^{
            [self loadDataWithPullup:false];
        }];
        MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            [self loadDataWithPullup:YES];
        }];
        _tableView.mj_header = header;
        _tableView.mj_footer = footer;
        
        [footer setTitle:@"没有更多了哦～" forState:MJRefreshStateNoMoreData];
        [footer setTitle:@"" forState:MJRefreshStateIdle];
        
        [_tableView registerNib:[UINib nibWithNibName:@"YTETAILogCell" bundle:nil] forCellReuseIdentifier:@"logCellID"];
        
        _tableView.tableFooterView = [[UIView alloc] init];
        
        _tableView.backgroundColor = [UIColor colorWithHex:@"f1f2f2"];
    }
    return _tableView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    [self.view addSubview:self.headerView];
    [self.view addSubview:self.tableView];
    
    self.page = 1;
    [self setupNav];
    
    [self.tableView.mj_header beginRefreshing];
}

#pragma mark: - 加载数据
- (void)loadDataWithPullup:(BOOL)pullUp
{
    if (pullUp) {
        self.page++;
        
        // 等于总条数 就没有更多了
        if (self.model.list.count == self.model.total.intValue) {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
            return;
        }
    }else{
        self.page = 1;
        self.tableView.mj_footer.state = MJRefreshStateIdle;
    }
    
    [YTNetworkManager.shareManager requestEtAILogListWithPaged:self.page
                                                     page_size:10
                                                    completion:^(YTETAILogModel *model, BOOL isSucc, NSString *error)
    {
        if (!isSucc) {
            [self.view showErrorWithTipStr:error];
            pullUp ? [self.tableView.mj_footer endRefreshing] : [self.tableView.mj_header endRefreshing];
            return ;
        }
        
        if (!pullUp) {
            self.model = model;
            [self.tableView.mj_header endRefreshing];
        }else{
            self.model.list = [self.model.list arrayByAddingObjectsFromArray:model.list];
            [self.tableView.mj_footer endRefreshing];
        }
        
        [self.tableView reloadData];
        
    }];
}

#pragma mark: - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.model.list.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    YTETAILogCell *cell = [tableView dequeueReusableCellWithIdentifier:@"logCellID" forIndexPath:indexPath];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.model = self.model.list[indexPath.row];
    return cell;
}


- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [self.navigationController.navigationBar setTintColor:UIColor.whiteColor];

}

- (void)setupNav
{
    [self.navigationController.navigationBar setTintColor:UIColor.blackColor];
    
    UILabel *titleLabel = [[UILabel alloc] init];
    titleLabel.text = @"星币明细";
    titleLabel.font = [UIFont systemFontOfSize:15];
    titleLabel.font = [UIFont boldSystemFontOfSize:15];
    titleLabel.textColor = UIColor.blackColor;
    self.navigationItem.titleView = titleLabel;
    
}



@end
